package com.itita.ww2.utils;

import java.util.Map;
import java.util.Map.Entry;

import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import com.itita.ww2.common.WebProxy;
import com.itita.ww2.common.WebResponse;
import com.itita.ww2.core.ServerService;
import com.itita.ww2.data.WorldMapConfig;
import com.itita.ww2.game.user.UserInventoryManager;
import com.itita.ww2.game.user.UserService;
import com.itita.ww2.model.user.UserProfile;

/**
 * 代理实现
 * @author NorthLan
 *
 */
public class WebProxyImpl implements WebProxy {

	//============================== Server =============================//
	@Override
	public WebResponse landingAllRedisData(boolean force) {
		WebResponse ret = new WebResponse(true);
		try {
			ServerService.getInstance().getRedis().syncToDB(force);
		} catch (Exception e) {
			return ret.setSucc(false).setErrorMsg(e.getMessage());
		}
		return ret;
	}

	@Override
	public WebResponse stopServer() {
		return new WebResponse();
	}

	//============================== User =============================//

	@Override
	public WebResponse getUserInfoByNickName(String nickname) {
		WebResponse ret = new WebResponse(true);
		return ret.setSucc(false).setErrorMsg("尼玛,传进来一个nickname: " + nickname + " 戳卵???");
	}

	@Override
	public WebResponse getUserInfoByUid(int uid) {
		WebResponse ret = new WebResponse(true);
		return ret.setSucc(false).setErrorMsg("尼玛,传进来一个uid: " + uid + " 戳卵???");
	}

	@Override
	public WebResponse kickUser(int uid) {
		WebResponse ret = new WebResponse();
		UserProfile userProfile = UserService.getInstance().getUserProfile(uid);
		if (userProfile == null) {
			return ret.setErrorMsg("userProfile not found!");
		}
		ServerService.getInstance().getMainExtension().getApi().kickUser(userProfile.getSFSUser(), null, "Kick", 0);
		ret.setSucc(true);
		return ret;
	}

	@Override
	public WebResponse addUserItem(int uid, String[] itemIds, int count) {
		WebResponse ret = new WebResponse(false);
		UserProfile userProfile = UserService.getInstance().getUserProfile(uid);
		if (userProfile == null) {
			return ret.setErrorMsg("userProfile not found!");
		}
		UserInventoryManager inventory = userProfile.getInventory();
		try {
			for (int i = 0; i < itemIds.length; ++i) {
				String itemId = itemIds[i];
				inventory.addItem(itemId, count);
			}
		} catch (Exception e) {
			inventory.rollback();
		} finally {
			inventory.clean();
		}
		inventory.update();
		inventory.sync();

		ret.setSucc(true);
		return ret;
	}

	@Override
	public WebResponse addUserResource(int uid, String resJson) {
		WebResponse ret = new WebResponse();
		UserProfile userProfile = UserService.getInstance().getUserProfile(uid);
		if (userProfile == null) {
			return ret.setErrorMsg("userProfile not found!");
		}
		//
		Map<String, Long> res = new GsonBuilder().create().fromJson(resJson, new TypeToken<Map<String, Long>>() {
		}.getType());
		// 
		for (Entry<String, Long> entry : res.entrySet()) {
			userProfile.getResManager().operRes(entry.getKey(), entry.getValue());
		}
		userProfile.getResManager().update();
		userProfile.getResManager().sync();
		//
		ret.setSucc(true);
		return ret;
	}

	@Override
	public WebResponse addUserFatigue(int uid, int value) {
		UserProfile userProfile = UserService.getInstance().getUserProfile(uid);
		userProfile.getUserInfo().fatigueOper(value);
		userProfile.getUserInfo().update();
		userProfile.getUserInfo().syncFatigue();
		return new WebResponse(true);
	}

	@Override
	public WebResponse resetUserFfCount(int uid) {
		UserProfile userProfile = UserService.getInstance().getUserProfile(uid);
		userProfile.getUserInfo().resetFfCount();
		userProfile.getUserInfo().update();
		userProfile.getUserInfo().syncFatigue();
		return new WebResponse(true);
	}

	//============================== WorldMap =============================//

	@Override
	public WebResponse resetWorldMapState(int blockId, int cityId, int baseId) {
		//		WorldMapService service = WorldMapService.getInstance();
		//		if (blockId == -1 && cityId == -1 && baseId == -1) {
		//			service.resetWorldMapState();
		//		} else if (blockId != -1 && cityId != -1 && baseId == -1) {
		//			service.resetCityState(blockId, cityId);
		//		} else if (blockId != -1 && cityId != -1 && baseId != -1) {
		//			service.resetBaseState(blockId, cityId, baseId);
		//		}
		//		return new WebResponse(true);
		return null;
	}

	@Override
	public WebResponse forceUnlockCityAndMission(int uid, int blockId, int cityId) {
		WebResponse ret = new WebResponse();
		UserProfile userProfile = UserService.getInstance().getUserProfile(uid);
		// 区块都没解锁(给他解锁)
		if (!userProfile.getWMManager().getUserUnlockDataMgr().isBlockUnlocked(blockId)) {
			if (!userProfile.getWMManager().getUserUnlockDataMgr().gmUnlockBlock(blockId)) {
				return ret;
			}
		}
		// 解锁城市
		if (!userProfile.getWMManager().getUserUnlockDataMgr().gmUnlockCity(blockId, cityId)) {
			return ret;
		}
		// Reward & UnlockData
		String missionId = WorldMapConfig.getCityMissionId(blockId, cityId);
		userProfile.getMissionManager().missionComplete(missionId, null);
		// TODO 数据刷新
		// MessageHandler.getInstance().pushMsg(PushCode.WORLDMAP_CITY_DATA_UPDATE, null, userProfile.getSFSUser());
		ret.setSucc(true);
		// 
		return ret;
	}
}
